﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

namespace WebApplication1
{
    public partial class DanhSachGioHang : System.Web.UI.Page
    {
        sv_QuanLyNhaSach.AdapterClient service = new sv_QuanLyNhaSach.AdapterClient();
        PagedDataSource pds = new PagedDataSource();
        String MaTKOL = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Browser.Cookies)
            {
                if (Request.Cookies.Get("Login") != null)
                {
                    HttpCookie MyCookies = Request.Cookies["Login"];
                    if (MyCookies["UserName"] != null)
                    {
                        MaTKOL = LayMaTK(MyCookies["UserName"].ToString());
                    }
                }
                else if (Session["UserName"] != null)
                {
                    MaTKOL = LayMaTK(Session["UserName"].ToString());
                }
            }
            
            if (MaTKOL != "")
            {
                Load_DSDH();

            }
            else
                Response.Redirect("TrangChu.aspx");
        }

        private void Load_DSDH()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select madhol,ngaydh,trangthai from dathangonline where matkol='" + MaTKOL + "' order by madhol desc")));
            pds.AllowPaging = true;
            pds.PageSize = 5;
            pds.CurrentPageIndex = 0;
            if (ds.Tables.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ds.Tables[0].Rows[i]["ngaydh"] = DateTime.Parse(ds.Tables[0].Rows[i]["ngaydh"].ToString()).Day.ToString("00") + "/" + DateTime.Parse(ds.Tables[0].Rows[i]["ngaydh"].ToString()).Month.ToString("00") + "/" + DateTime.Parse(ds.Tables[0].Rows[i]["ngaydh"].ToString()).Year + "    " + DateTime.Parse(ds.Tables[0].Rows[i]["ngaydh"].ToString()).Hour.ToString("00") + ":" + DateTime.Parse(ds.Tables[0].Rows[i]["ngaydh"].ToString()).Minute.ToString("00");
                    if (ds.Tables[0].Rows[i]["trangthai"].ToString().Trim() == "DG")
                    {
                        ds.Tables[0].Rows[i]["trangthai"] = "Đã giao";
                    }
                    else if (ds.Tables[0].Rows[i]["trangthai"].ToString().Trim() == "CXN")
                    {
                        ds.Tables[0].Rows[i]["trangthai"] = "Chưa xác nhận";
                    }
                    else
                    {
                        ds.Tables[0].Rows[i]["trangthai"] = "Đã xác nhận";
                    }
                }
                pds.DataSource = ds.Tables[0].DefaultView;
                Panel3.Controls.Clear();
                Label lbl = new Label();
                lbl.Text = "Trang: ";
                Panel3.Controls.Add(lbl);
                for (int i = 0; i < pds.PageCount; i++)
                {
                    LinkButton btn = new LinkButton();
                    btn.Text = (i + 1).ToString();
                    btn.Click += new EventHandler(btn_Click);
                    btn.Width = 15;
                    Panel3.Controls.Add(btn);
                }
                if (!IsPostBack)
                {
                    dl_dsgiohang.DataSource = pds;
                    dl_dsgiohang.DataBind();
                }
            }
        }
        private void Load_DSDH1()
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select madhol,ngaydh,trangthai from dathangonline where matkol='" + MaTKOL + "' order by madhol desc")));
            pds.AllowPaging = true;
            pds.PageSize = 5;
            pds.CurrentPageIndex = 0;
            if (ds.Tables.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ds.Tables[0].Rows[i]["ngaydh"] = DateTime.Parse(ds.Tables[0].Rows[i]["ngaydh"].ToString()).Day.ToString("00") + "/" + DateTime.Parse(ds.Tables[0].Rows[i]["ngaydh"].ToString()).Month.ToString("00") + "/" + DateTime.Parse(ds.Tables[0].Rows[i]["ngaydh"].ToString()).Year + "    " + DateTime.Parse(ds.Tables[0].Rows[i]["ngaydh"].ToString()).Hour.ToString("00") + ":" + DateTime.Parse(ds.Tables[0].Rows[i]["ngaydh"].ToString()).Minute.ToString("00");
                    if (ds.Tables[0].Rows[i]["trangthai"].ToString()=="DG")
                    {
                        ds.Tables[0].Rows[i]["trangthai"] = "Đã giao";
                    }
                    else if (ds.Tables[0].Rows[i]["trangthai"].ToString()=="CXN")
                    {
                        ds.Tables[0].Rows[i]["trangthai"] = "Chưa xác nhận";
                    }
                    else 
                    {
                        ds.Tables[0].Rows[i]["trangthai"] = "Đã xác nhận";
                    }
                }
                pds.DataSource = ds.Tables[0].DefaultView;
                Panel3.Controls.Clear();
                Label lbl = new Label();
                lbl.Text = "Trang: ";
                Panel3.Controls.Add(lbl);
                for (int i = 0; i < pds.PageCount; i++)
                {
                    LinkButton btn = new LinkButton();
                    btn.Text = (i + 1).ToString();
                    btn.Click += new EventHandler(btn_Click);
                    btn.Width = 15;
                    Panel3.Controls.Add(btn);
                }
                    dl_dsgiohang.DataSource = pds;
                    dl_dsgiohang.DataBind();
            }
        }
        private string LayMaTK(string p)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select matkol from TaiKhoanOnline where TenDangNhap='" + p + "' ")));
            if (ds.Tables.Count > 0)
            {
                return ds.Tables[0].Rows[0][0].ToString();
            }
            return "";
        }
        void btn_Click(object sender, EventArgs e)
        {
            int trang = int.Parse(((LinkButton)sender).Text) - 1;
            pds.CurrentPageIndex = trang;
            dl_dsgiohang.DataSource = pds;
            dl_dsgiohang.DataBind();
        }
        private bool ktquyen(string madh)
        {
            DataSet ds = new DataSet();
            if (Session["UserName"] !="")
            {
                ds.ReadXml(new StringReader(service.selectqueryByText("select madhol from dathangonline where madhol='"+madh+"' and matkol='"+MaTKOL+"'")));
                if (ds.Tables.Count>0)
                {
                    return true;
                }
            }
            return false;
        }
        protected void dl_dsgiohang_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName=="Xem")
            {
                //Label lb = (Label)e.Item.FindControl("Label2");
                //string tt;
                //if (lb.Text == "chuagiao")
                //    tt = "false";
                //else
                //    tt = "true";
                //Response.Redirect("GioHang.aspx?SoHD=" + e.CommandArgument.ToString()+"&TT="+tt);
                Response.Redirect("GioHang.aspx?SoHD=" + e.CommandArgument.ToString());
            }
            else if (e.CommandName=="xoa")
            {
                if (ktquyen(e.CommandArgument.ToString())==true)
                {
                    if (XetTrangThai(e.CommandArgument.ToString()) == true)
                    {
                        service.selectqueryByText("delete from CTDatHangONL where madhol='" + e.CommandArgument.ToString() + "'");
                        service.selectqueryByText("delete from dathangonline where madhol='" + e.CommandArgument.ToString() + "'");
                        Load_DSDH1();
                    }
                    else
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Bạn không thể sửa đơn đặt hàng đã giao.');location.href='../DanhSachGioHang.aspx';", true);
                }
            }
        }
        private bool XetTrangThai(string madhol)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select trangthai from DatHangOnline where MaDHOL ='" + madhol + "'")));
            if (ds.Tables.Count > 0)
            {
                if (ds.Tables[0].Rows[0][0].ToString() == "Chưa giao")
                    return true;
            }
            return false;
        }
    }
}